// 导入用到的包
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import type { RootState } from "@/types/store";

// 创建 useInitialState 函数（自定义 hook）
const useInitialState = <StateName extends keyof RootState>(
  action: () => void,
  stateName: StateName
) => {
  const dispatch: any = useDispatch();
  const state = useSelector((state: RootState) => state[stateName]);

  useEffect(() => {
    dispatch(action());
  }, [dispatch, action]);

  return state;
};

export { useInitialState };
